/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:26 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class Cauhoi_DekiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Cauhoi_Dekiemtra Này";
		private static string EX_FK_IDEKIEMTRAID_INVALID="FK_iDekiemtraID không hợp lệ";
		private static string EX_FK_ICAUHOIID_INVALID="FK_iCauhoiID không hợp lệ";
		private static string EX_ID_INVALID="FK_iDekiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Cauhoi_Dekiemtra Theo ID
        /// </summary>
        /// <param name="FK_iDekiemtraID">Int64:Cauhoi_Dekiemtra ID</param>
        /// <returns>Cauhoi_DekiemtraEntity</returns>        
        public static Cauhoi_DekiemtraEntity GetOne(Int64 FK_iDekiemtraID)
        {
            
			if(FK_iDekiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return Cauhoi_DekiemtraDAL.GetOne(FK_iDekiemtraID);
        }
        /// <summary>
        /// Lấy về List các Cauhoi_Dekiemtra
        /// </summary>
        /// <returns>List Cauhoi_DekiemtraEntity:List Cauhoi_Dekiemtra Cần lấy</returns>
        public static List<Cauhoi_DekiemtraEntity> GetAll()
        {
            return Cauhoi_DekiemtraDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Cauhoi_Dekiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Cauhoi_Dekiemtra Mới Thêm Vào</returns>
        public static Int32 Add(Cauhoi_DekiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return Cauhoi_DekiemtraDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Cauhoi_Dekiemtra
        /// </summary>
        /// <param name="entity">Cauhoi_DekiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(Cauhoi_DekiemtraEntity entity)
        {
            checkExist(entity.FK_iDekiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return Cauhoi_DekiemtraDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Cauhoi_Dekiemtra
        /// </summary>
        /// <param name="FK_iDekiemtraID">Int64 : FK_iDekiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 FK_iDekiemtraID)
        {
            checkExist(FK_iDekiemtraID);
            return Cauhoi_DekiemtraDAL.Remove(FK_iDekiemtraID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FK_iKhoahocID"></param>
        /// <returns></returns>
        public static List<Cauhoi_DekiemtraEntity> GetByFK_iDekiemtraID(Int64 FK_iDekiemtraID)
        {
            if (FK_iDekiemtraID <= 0)
                throw new Exception(EX_FK_ICAUHOIID_INVALID);
            return Cauhoi_DekiemtraDAL.GetByFK_iDekiemtraID(FK_iDekiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 FK_iDekiemtraID)
        {
            Cauhoi_DekiemtraEntity oCauhoi_Dekiemtra=Cauhoi_DekiemtraDAL.GetOne(FK_iDekiemtraID);
            if(oCauhoi_Dekiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">Cauhoi_DekiemtraEntity: entity</param>
        private static void checkLogic(Cauhoi_DekiemtraEntity entity)
        {   
			if (entity.FK_iDekiemtraID < 0)
				throw new Exception(EX_FK_IDEKIEMTRAID_INVALID);
			if (entity.FK_iCauhoiID < 0)
				throw new Exception(EX_FK_ICAUHOIID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">Cauhoi_DekiemtraEntity: Cauhoi_DekiemtraEntity</param>
        private static void checkDuplicate(Cauhoi_DekiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<Cauhoi_DekiemtraEntity> list = Cauhoi_DekiemtraDAL.GetAll();
            if (list.Exists(
                delegate(Cauhoi_DekiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iDekiemtraID != entity.FK_iDekiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">Cauhoi_DekiemtraEntity:entity</param>
        private static void checkFK(Cauhoi_DekiemtraEntity entity)
        {            
        }
        #endregion
    }
}
